#ifndef _EVENT_HPP
#define _EVENT_HPP

// #include "wifi.h"

void eventInit();
void eventStart();


void eventInit() {
	if(eventGroup == NULL) {
		log("Event Group create fail!");
	}
	else {
		// BaseType_t res = xTaskCreatePinnedToCore(eventStart, "eventStart", MEM_UNIT * 6, NULL, 0, NULL, 0);
		xTaskCreatePinnedToCore(eventStart, "eventStart", MEM_UNIT * 6, NULL, 0, NULL, 0);
	}
}


void eventStart() {
	EventBits_t bits;
	while(1) {
		bits = xEventGroupWaitBits(
			eventGroup,
			EVENT_SCREEN_INIT_DONE |
			EVENT_WIFI_AP_START |
			EVENT_WIFI_CONNECTED |
			EVENT_WIFI_DISCONNECTED |
			EVENT_WIFI_GOT_IP |
			EVENT_WS_CONNECTED |
			EVENT_WS_DISCONNECTED,
			true,
			false, 
			portMAX_DELAY
		);
		// 屏幕初始化完成
		if(bits & EVENT_SCREEN_INIT_DONE) {
			// 初始化页面
			pageInit();
			pageInfoInit();
			pageHomeInit();
			pageSettingsInit();
			
			pageShow(PAGE_INFO);
			dispInitInfo("UI init done.");
			dispInitInfo("Pages init done.");
			// 初始化扬声器
			beeInit();
			// 初始化nvs
			nvsInit();
			// 初始化软键盘
			keyboradInit();

			// pageShow(PAGE_HOME);
			stGroup[ST_KEY_TYPE].intValue = 1;
			stGroup[ST_DI_TIME].intValue = 110;
			saveSettings();
			// printSettings();
			
			// 初始化wifi
			xTaskCreatePinnedToCore(wifiInitTask, "wifiInitTask", MEM_UNIT * 5, NULL, 0, NULL, 0);
			// 初始化二维码扫码页面
			xTaskCreatePinnedToCore(qrCodeInitTask, "qrCodeInitTask", MEM_UNIT * 3, NULL, 0, NULL, 1);
		}
		// wifi ap模式开启
		if(bits & EVENT_WIFI_AP_START) {
			log("event EVENT_WIFI_AP_START");
			statusSet(STATUS_WIFI_AP_OK);
			char *ip[25] = {0};
			sprintf((char *)ip, "(AP: %s)", (char *)apIpStr);
			statusBarInfoUpdate((char *)ip);
			if(!stGroup[ST_USE_NET].intValue) {
				if(pageCurrent(PAGE_INFO)) {
					DELAY(100);
					pageShow(PAGE_HOME);
				}
			}
		}
		// wifiAP模式停止
		else if(bits & EVENT_WIFI_AP_STOP) {
			log("event EVENT_WIFI_AP_STOP");
			statusClear(STATUS_WIFI_AP_OK);
		}
		// wifi已经连接上指定ssid
		else if(bits & EVENT_WIFI_CONNECTED) {
			log("event EVENT_WIFI_CONNECTED");
			statusSet(STATUS_WIFI_CONNECTED);
			statusBarIconStatus("wifi", true);
			dispInitInfo("Getting IP...");
		}
		// wifi与指定ssid断开连接
		else if(bits & EVENT_WIFI_DISCONNECTED) {
			log("event EVENT_WIFI_DISCONNECTED");
			xEventGroupClearBits(eventGroup, EVENT_WIFI_GOT_IP);
			statusClear(STATUS_WIFI_CONNECTED);
			statusBarIconStatus("wifi", false);
			xTaskCreatePinnedToCore(wifiReConnectTask, "wifiRC", MEM_UNIT * 4, NULL, 0, NULL, 0);
		}
		// WIFI获取到IP
		else if(bits & EVENT_WIFI_GOT_IP) {
			log("event EVENT_WIFI_GOT_IP");
			statusSet(STATUS_WIFI_CONNECTED);
			char *ip[25] = {0};
			sprintf((char *)ip, "(STA: %s)", (char *)staIpStr);
			statusBarInfoUpdate((char *)ip);
			dispInitInfo("Got IP.");
			// 连接WS
			if(stGroup[ST_USE_NET].intValue && statusCheck(STATUS_WIFI_CONNECTED)) {
				dispInitInfo("Try to connect WebSocket...");
				if(!statusCheck(STATUS_WS_INIT_DONE)) {
					xTaskCreatePinnedToCore(wsInitTask, "wsInitTask", MEM_UNIT * 6, NULL, 0, NULL, 0);
					// wsInit();
				}
				else {
					xTaskCreatePinnedToCore(wsReStartTask, "wsReStartTask", MEM_UNIT * 2, NULL, 0, NULL, 0);
				}
			}
		}
		// ws连接成功
		else if(bits & EVENT_WS_CONNECTED) {
			log("event EVENT_WS_CONNECTED");
			statusSet(STATUS_WS_CONNECTED);
			statusBarIconStatus("ws", true);
			// 如果在自检页面，则转到首页
			if(pageCurrent(PAGE_INFO)) {
				DELAY(300);
				pageShow(PAGE_HOME);
			}
			// WebSocket发送操作
			// BaseType_t res = xTaskCreatePinnedToCore(wsSendOp, "wsSendOp", MEM_UNIT * 5, NULL, 0, &wsSendOpH, 0);
			// log("wsSendOp start res = %d", res);
			xTaskCreatePinnedToCore(wsSendOp, "wsSendOp", MEM_UNIT * 5, NULL, 0, &wsSendOpH, 0);
		}
		// ws连接断开
		else if(bits & EVENT_WS_DISCONNECTED) {
			log("event EVENT_WS_DISCONNECTED");
			statusClear(STATUS_WS_CONNECTED);
			statusBarIconStatus("ws", false);
			if(stGroup[ST_USE_NET].intValue) {
				if(statusCheck(STATUS_WIFI_CONNECTED)) {
					xTaskCreatePinnedToCore(wsStartTask, "wsStartTask", MEM_UNIT * 5, NULL, 0, NULL, 0 );
				}
				else {
					log("WS D, WIFI NOT DONE!!!");
				}
			}
			else {
				log("WS DISCONNECTED AND NOT USE NET");
			}
		}
	}
}

#endif
